/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.parse;

import acid.spring.GeneAliasService;
import acid.spring.GeneExpressionResultService;
import acid.spring.db.ExperimentAnalysis;
import acid.spring.db.GeneAlias;
import acid.spring.db.Organism;
import acid.spring.db.Tissue;
import java.io.IOException;
import java.util.List;
import javax.swing.JOptionPane;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.EmptyResultDataAccessException;

/**
 *
 * @author morgan
 */
public class AddExpressionData
{

    private GetAnalysisFilesPanel analysisPanel;
    private GeneAliasService geneAliasService;
    private GeneExpressionResultService resultService;

    public static void main(String[] args) throws Exception
    {
        try
        {
            AddExpressionData aed = new AddExpressionData();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public AddExpressionData() throws IOException
    {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:/jdbc-services.xml");
        BeanFactory factory = context;
        geneAliasService = (GeneAliasService) factory.getBean(GeneAliasService.class);
        resultService = (GeneExpressionResultService) factory.getBean(GeneExpressionResultService.class);

        boolean addAnother = true;
        while (addAnother)
        {
            addAnother = false;
            analysisPanel = new GetAnalysisFilesPanel();
            int i = JOptionPane.showOptionDialog(null, analysisPanel,
                    "Add Expression Data", JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.PLAIN_MESSAGE, null,
                    new String[]
                    {
                        "OK", "OK, Add Another", "Cancel"
                    }, null);
            if (i != JOptionPane.CANCEL_OPTION)
            {
                addData();
            }
            if (i == JOptionPane.NO_OPTION)
            {
                addAnother = true;
            }
        }
    }

    private void addData() throws IOException
    {
        ExperimentAnalysis analysis = (ExperimentAnalysis) analysisPanel.getAnalysisBox().getSelectedItem();
        Tissue tissue = (Tissue) analysisPanel.getTissueBox().getSelectedItem();
        String filename = analysisPanel.getFileTextField().getText();
        Organism organism = (Organism) analysisPanel.getOrganismBox().getSelectedItem();
        List<GANRow> ganRows = null;
        try
        {
            ganRows = ParseGANResults.parse(filename);
        }
        catch (IOException e)
        {
            try
            {
                ganRows = ParseTopTableResults.parse(filename);
            }
            catch (IOException e2)
            {
                ganRows = ParseRudolphResults.parse(filename);
            }
        }
        int total = ganRows.size(), counter = 0;
        for (GANRow row : ganRows)
        {
            String gene = row.getGeneName();
            GeneAlias ge = null;
            try
            {
                ge = geneAliasService.getGeneAliasByAlias(gene);
            }
            catch (EmptyResultDataAccessException e)
            {
                ge = geneAliasService.createGeneAlias(row.getGeneId(), gene,
                        organism);
            }
            resultService.createGeneExpressionResult(ge, analysis, organism,
                    tissue, row.getFoldChange(), row.getPVal());

            System.out.println(counter++ * 100 / total + "%");
        }
    }
}
